let storage = bui.storage();
let language
let translate
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            if (language === "chinese_simplified") {
                router.$('#register').css("font-size", ".3rem");
                router.$('#loginFooter').css("font-size", "");
            } else {
                router.$('#register').css("font-size", ".2rem");
                router.$('#loginFooter').css("font-size", ".18rem");
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            let uiLoading1 = bui.loading({
                appendTo:"#loadingLogin",
                width: 40,
                height: 40,
                opacity: 0.5,
                text:"登录中。。。",
                autoClose: false
            });

            let params = module.props;
            router.$("#loginFh").click(function () {
                switch (params.tzSign) {
                    case "0":
                        storage.set("tzPage", 0)
                        router.replace({url: "pages/mainIndex/mainIndex.html"});
                        break
                    case "1":
                        let article = storage.get("tzArticleInfo",0)
                        router.replace({ url: "pages/articleInfo/articleInfo.html", param: {id: article.id, glUserid: article.glUserid}});
                        break
                }
            })

            let dlTz = storage.get("dlTz", 0)
            let accountStatus = false
            let passwordStatus = false
            let org = storage.get("org",0)
            if (org) {
                router.$("#cs").html(org.description)
            }

            bui.input({
                id: ".user-input",
                showIcon: false,
                onInput: function(e) {
                    e.target.value = e.target.value.trim()
                    if (!e.target.value) {
                        accountStatus = false
                        router.$("#accountTs").html("账号不可为空");
                        return false;
                    } else {
                        accountStatus = true
                        router.$("#accountTs").html("");
                    }
                    return true;
                }
            })
            // 密码显示或者隐藏
            bui.input({
                id: "#passwordInput",
                iconClass: ".icon-eye",
                onInput: function(e) {
                    e.target.value = e.target.value.trim()
                    // 注册的时候校验只能4-18位密码
                    let rule = /^[a-zA-Z0-9_-]{4,18}$/;
                    if (!rule.test(e.target.value)) {
                        passwordStatus = false
                        router.$("#passwordTs").html("密码只能4-18位")
                        return false;
                    } else {
                        passwordStatus = true
                        router.$("#passwordTs").html("")
                    }
                    return true;
                },
                callback: function(e) {
                    //切换类型
                    this.toggleType();
                    //
                    router.$(e.target).toggleClass("active")
                }
            })

            router.$("#login").click(function () {
               if (accountStatus && passwordStatus) {
                   uiLoading1.show()
                   // bui.btn("#login").submit(function(loading) {
                       //查询菜单
                       bui.ajax({
                           url: global.api.login,
                           data: {username: router.$("#account").val(), password: router.$("#password").val()},
                           method: "GET"
                       }).then(function(res){
                           let data = res
                           if (data.code == '200') {
                               storage.set("token", data.token)
                               storage.set("user", data)

                               if (dlTz == '0') {
                                   router.replace({ url: "pages/fbZj/fbZj.html"});
                               } else if (dlTz == '1') {
                                   router.replace({ url: "pages/articleInfo/articleInfo.html"});
                               }else if (dlTz == '2') {
                                   storage.set("tzPage", '2')
                                   router.replace({ url: "pages/mainIndex/mainIndex.html"});
                               }else if (dlTz == '9') {
                                   router.replace({ url: "pages/fbZjGzh/fbZjGzh.html"});
                               }else if (dlTz == '10') {
                                   router.replace({ url: "pages/articleEdit/articleEdit.html"});
                               }else if (dlTz == '11') {
                                   router.replace({ url: "pages/articleList/articleList.html"});
                               }else if (dlTz == '12') {
                                   router.replace({ url: "pages/commentList/commentList.html"});
                               }else if (dlTz == '13') {
                                   router.replace({ url: "pages/userManager/userManager.html"});
                               }else if (dlTz == '14') {
                                   router.replace({ url: "pages/endEdit/endEdit.html"});
                               }else if (dlTz == '16') {
                                   router.replace({ url: "pages/advertiseList/advertiseList.html"});
                               }else if (dlTz == '15') {
                                   router.replace({ url: "pages/audit/audit.html"});
                               }else if (dlTz == '17') {
                                   router.replace({ url: "pages/auditComment/auditComment.html"});
                               }else if (dlTz == '18') {
                                   router.replace({ url: "pages/dashboard/dashboard.html"});
                               }else {
                                   router.replace({ url: "pages/mainIndex/mainIndex.html"});
                               }
                           } else {
                               bui.alert(data.msg)
                           }
                           uiLoading1.stop()
                           // loading.stop()
                       })
                   // })
               } else {
                   if (!accountStatus) {
                       router.$("#accountTs").html("账号不可为空");
                   }
                   if (!passwordStatus) {
                       router.$("#passwordTs").html("密码只能4-18位");
                   }
                   bui.hint("账号密码不符合规则，请检查后重试")
               }
            })
            router.$("#wxLogin").click(function () {
               if (isWeiXin()) {
                   location.href = global.api.gzhAuthor
               } else {
                   location.href = global.api.htmlAuthor
               }
            })
            router.$("#register").click(function () {
                bui.load({ url: "pages/register/register.html", param: {tzSign: params.tzSign}});
            })
        }
    }
    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})

function isWeiXin() {
    let ua = navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true
    } else {
        return false
    }
}
